clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
use if qruf1<. using $Work/EBR_new_monthly, clear
tsset permno datem

* compute actual returns
generate   retQr1=1

forvalues x=1(1)5 {
generate   retYr`x'=1
}

generate  retMo1=ret_mo1
forvalues x=1(1)3 {
replace   retQr1=retQr1*(ret_mo`x')
}
forvalues x=1(1)12 {
replace   retYr1=retYr1*(ret_mo`x')
}
forvalues x=13(1)24 {
replace   retYr2=retYr2*(ret_mo`x')
}
forvalues x=25(1)36 {
replace   retYr3=retYr3*(ret_mo`x')
}
forvalues x=37(1)48 {
replace   retYr4=retYr4*(ret_mo`x')
}
forvalues x=49(1)60 {
replace   retYr5=retYr5*(ret_mo`x')
}
* *************************
scalar alfa= 0.97749304
gen cumRetNewMo1=ln(retMo1) 
gen cumRetNewQr1=ln(retQr1) 
gen cumRetNewYr1=ln(retYr1) 
gen cumRetNewYr3=ln(retYr1)+alfa*ln(retYr2)+alfa^2*ln(retYr3) 
gen cumRetNewYr5=ln(retYr1)+alfa*ln(retYr2)+alfa^2*ln(retYr3)+alfa^3*ln(retYr4)+alfa^4*ln(retYr5)
* **************************
gen cumQNRetMo1=ln(1+qnic1 ) 
gen cumQNRetQr1=ln(qnic_qr1) 
gen cumQNRetYr1=ln(qnic_yr1) 
gen cumQNRetYr3=ln(qnic_yr1)+alfa*ln(qnic_yr2)+alfa^2*ln(qnic_yr3) 
gen cumQNRetYr5=ln(qnic_yr1)+alfa*ln(qnic_yr2)+alfa^2*ln(qnic_yr3)+alfa^3*ln(qnic_yr4)+alfa^4*ln(qnic_yr5)
gstats winsor cumQN*, cuts(1 99) by(datem) replace 
* **************************
keep permco permno datem cum*
tempfile cumQ
save "`cumQ'", replace
* **************************
use using $Work/Firms01_June, clear
merge 1:1 permco permno datem using "`cumQ'"
keep if _merge==3
drop _merge

* add book-to-market from read_compustat_qtly.do
merge 1:1 permco permno datem using $Work/bemeLTM, keepusing(permco permno datem bemeLTM)
drop if _merge==2
drop _merge
gen lnbeme=ln(bemeLTM)

* add mcap
merge 1:1 permco permno datem using $Work/ibes_ret, keepusing(permco permno datem mcap)
drop if _merge==2
drop _merge

bys datem permco: egen Tot=total(mcap)
gen lnmcap=ln(Tot)
* ---------------------------------------------- *
keep if LTG+BOOK+SIZE+cumQNRetMo1<.
* ---------------------------------------------- *
* winsorize @ 1% and 99%
gstats winsor cumQ* op inv lnbeme L1LTG d1LLTG STG2, cuts(1 99) by(datem) replace  
keep permco permno datem cum* op inv lnbeme Tot L12ret L1LTG d1LLTG STG2 d1LLTGagg L1LTGagg
keep if inv+lnbeme<.
* ***************************
tsset permco datem
* ***************************
gen lnmcap=ln(Tot)

local X "lnbeme lnmcap inv op L12ret"
foreach var of varlist `X'  {
	quietly: egen sd=sd(`var')
	quietly: replace `var'=`var'/sd	
	drop sd
}
save $Work/Table09_June.dta, replace
* *****************************
* Table 9 with firm fxd effect
* *****************************
eststo clear
quietly: eststo: ivreghdfe 	cumQNRetMo1 	`X' , a(permco, resid) dkraay(1)
quietly: eststo: ivreghdfe 	cumQNRetQr1	`X' , a(permco, resid) dkraay(3) 
 
forvalues x=1(2)5 {
local lag=12*`x'+1
quietly: eststo: ivreghdfe 	cumQNRetYr`x' 	`X' , a(permco, resid) dkraay(`lag')  
}
local file "$Tables/Table09_June.csv"
esttab , nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2 r2_a F) 
esttab using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2 r2_a F)  title(permco) replace
